package com.xckj.ibox.ui.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;

import com.xckj.ibox.R;
import com.xckj.ibox.bean.LoginBean;
import com.xckj.ibox.databinding.ActivityLoginBinding;
import com.xckj.ibox.framework.BaseActivity;
import com.xckj.ibox.global.Constant;
import com.xckj.ibox.iview.ILoginView;
import com.xckj.ibox.iview.ISendCodeView;
import com.xckj.ibox.presenter.LoginPresenter;
import com.xckj.ibox.presenter.SendCodePresenter;
import com.xckj.ibox.utils.AndroidUtil;
import com.xckj.ibox.utils.SpUtil;

import java.util.Objects;

/**
 * @Author: ChenYao
 * @Describe: 登录
 * @Date: 2023/6/10 21:47
 */
public class LoginActivity extends BaseActivity<ActivityLoginBinding> implements ISendCodeView, ILoginView {
    private SendCodePresenter sendCodePresenter;
    private LoginPresenter loginPresenter;
    private String permanent = "0";

    @Override
    protected boolean isStatusBarDarkFont() {
        return false;
    }

    @Override
    protected int loadLayout() {
        return R.layout.activity_login;
    }

    @Override
    protected void initData() {
        sendCodePresenter = new SendCodePresenter(this);
        loginPresenter = new LoginPresenter(this);
        setTextStyle();
        initAgreement();
    }

    private void setTextStyle() {
        dataBinding.etMobile.setTypeface(Typeface.DEFAULT_BOLD);
        dataBinding.etCode.setTypeface(Typeface.DEFAULT_BOLD);
    }

    private void initAgreement() {
        String tips1 = getString(R.string.privacy_1);
        String tips2 = getString(R.string.privacy_2);
        String tips = getString(R.string.privacy_tips, tips1, tips2);
        SpannableStringBuilder stringBuilder = new SpannableStringBuilder(tips);
        int color = Color.parseColor("#B462FF");
        int index = tips.indexOf(tips1);
        stringBuilder.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View view) {
                BrowserActivity.start(LoginActivity.this, SpUtil.getString(LoginActivity.this, Constant.Agreement.AGREEMENT_2));
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                ds.setColor(color);
            }
        }, index, index + tips1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        index = tips.indexOf(tips2);
        stringBuilder.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                BrowserActivity.start(LoginActivity.this, SpUtil.getString(LoginActivity.this, Constant.Agreement.AGREEMENT_3));
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                ds.setColor(color);
            }
        }, index, index + tips2.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        dataBinding.tvAgree.setText(stringBuilder);
        dataBinding.tvAgree.setMovementMethod(LinkMovementMethod.getInstance());
    }

    @Override
    protected void setListener() {
        dataBinding.sendCode.setOnClickListener(this);
        dataBinding.btnLogin.setOnClickListener(this);
        dataBinding.cbAutoLogin.setOnCheckedChangeListener((compoundButton, b) -> {
            if (b) {
                permanent = "1";
            } else {
                permanent = "0";
            }
        });
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View view) {
        String phone = Objects.requireNonNull(dataBinding.etMobile.getText()).toString().trim();
        String code = Objects.requireNonNull(dataBinding.etCode.getText()).toString().trim();
        if (AndroidUtil.isFastDoubleClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.send_code:
                sendCode(phone);
                break;
            case R.id.btn_login:
                login(phone, code);
                break;
            default:
                break;
        }
    }

    /**
     * 发送验证码
     *
     * @param phone 手机号
     */
    private void sendCode(String phone) {
        if (TextUtils.isEmpty(phone)) {
            showMsg(getString(R.string.phone_empty_hint));
            return;
        }
        if (AndroidUtil.isPhoneNumber(phone)) {
            showMsg(getString(R.string.phone_error_hint));
            return;
        }
        sendCodePresenter.sendCode(phone);
    }

    @Override
    public void sendSuccess() {
        showMsg(getString(R.string.send_code_hint));
        dataBinding.sendCode.start();
    }

    @Override
    public void sendFailed(String message) {
        showMsg(message);
    }


    /**
     * 登录
     *
     * @param phone 手机号码
     * @param code  验证码
     */
    private void login(String phone, String code) {
        if (TextUtils.isEmpty(phone)) {
            showMsg(getString(R.string.phone_empty_hint));
            return;
        }
        if (AndroidUtil.isPhoneNumber(phone)) {
            showMsg(getString(R.string.phone_error_hint));
            return;
        }
        if (TextUtils.isEmpty(code)) {
            showMsg(getString(R.string.code_empty_hint));
            return;
        }
        if (!dataBinding.cbAgree.isChecked()) {
            showMsg(getString(R.string.privacy_agree, getString(R.string.privacy_1), getString(R.string.privacy_2)));
            return;
        }
        Log.e("CY_", permanent);
        loginPresenter.login(phone, code, permanent);
    }

    @Override
    public void loginSuccess(LoginBean loginBean) {
        SpUtil.put(this, Constant.APP_TOKEN, loginBean.app_token);
        SpUtil.put(this, Constant.IS_AUTO_LOGIN, permanent);
        SpUtil.put(this, Constant.TOKEN, loginBean.userinfo.token);
        startActivity(new Intent(this, HomeActivity.class));
        finish();
    }

    @Override
    public void loginFailed(String message) {
        showMsg(message);
    }
}
